/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.adgroup_criterion_service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.XMLUtils;

public class AdGroupCriterionResponseParser {
	Document _XMLDocument;
	
	
	/*
	 * Constructor
	 */
	public AdGroupCriterionResponseParser(String XMLData){
		try {
			_XMLDocument = XMLUtils.XMLStringToDoc(XMLData);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}//Constructor
	
	
	
	/*
	 * Check response for errors
	 */
	public String checkForErrors(){
		String error = null;
		try {
	        NodeList faultstring = _XMLDocument.getElementsByTagName("faultstring");
	        error =  XMLUtils.getCharacterDataFromElement((Element) faultstring.item(0));
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return error;
	}//checkForErrors
	
	
	
	/*
	 * Parse keyword list data
	 */
	public ArrayList<HashMap<String, String>> getKeywordList(){
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		try {
	        
			Act_Super.resetTotals();
			
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
	        
	        for (int i = 0; i < nodes.getLength(); i++) {
                //fill up hush map for list population
            	HashMap<String, String> temp = new HashMap<String, String>();

	        	Element element = (Element) nodes.item(i);

	        	NodeList id = element.getElementsByTagName("id");
	        	temp.put("id", XMLUtils.getCharacterDataFromElement((Element) id.item(0)));
	        	
	        	NodeList text = element.getElementsByTagName("text");
	        	temp.put("text", XMLUtils.getCharacterDataFromElement((Element) text.item(0)));

	        	NodeList matchType = element.getElementsByTagName("matchType");
	        	temp.put("matchType", XMLUtils.getCharacterDataFromElement((Element) matchType.item(0)));
	
	        	NodeList userStatus = element.getElementsByTagName("userStatus");
	        	temp.put("userStatus", XMLUtils.getCharacterDataFromElement((Element) userStatus.item(0)));

	        	NodeList network = element.getElementsByTagName("network");
	        	temp.put("network", XMLUtils.getCharacterDataFromElement((Element) network.item(0)));

	        	NodeList clicks = element.getElementsByTagName("clicks");
	        	temp.put("clicks", XMLUtils.getCharacterDataFromElement((Element) clicks.item(0)));
	
	        	NodeList ctr = element.getElementsByTagName("ctr");
	        	temp.put("ctr", Converter.formatStrNumberPercent(
	        			XMLUtils.getCharacterDataFromElement((Element) ctr.item(0))
	        			));
	        	
	        	NodeList cost = element.getElementsByTagName("cost");
	        	Element costElement = (Element) cost.item(0);
		        NodeList costAmount = costElement.getElementsByTagName("microAmount");
		        temp.put("cost", Converter.microamountToStr(
		        			XMLUtils.getCharacterDataFromElement((Element) costAmount.item(0))
		        		));

	        	
	        	NodeList avgCpc = element.getElementsByTagName("averageCpc");
	        	Element avgCpcElement = (Element) avgCpc.item(0);
		        NodeList avgCpcAmount = avgCpcElement.getElementsByTagName("microAmount");
		        temp.put("avgcpc", Converter.microamountToStr(
		        			XMLUtils.getCharacterDataFromElement((Element) avgCpcAmount.item(0))
		        		));

	        	NodeList impressions = element.getElementsByTagName("impressions");
	        	temp.put("impressions", XMLUtils.getCharacterDataFromElement((Element) impressions.item(0)));
	        	
	        	Act_Super.uppendStatsToTotal(temp);
	        	
	        	list.add(temp);
	        }
	        
	        Act_Super.calculateTotalStats();
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }

		return list;
	}//getKeywordList	

	
	/*
	 * Parse keyword list data
	 */
	public ArrayList<HashMap<String, String>> getNegativeKeywordList(){
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		try {

			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
	        
	        for (int i = 0; i < nodes.getLength(); i++) {
                //fill up hush map for list population
            	HashMap<String, String> temp = new HashMap<String, String>();

	        	Element element = (Element) nodes.item(i);

	        	NodeList id = element.getElementsByTagName("id");
	        	temp.put("id", XMLUtils.getCharacterDataFromElement((Element) id.item(0)));
	        	
	        	NodeList text = element.getElementsByTagName("text");
	        	temp.put("text", XMLUtils.getCharacterDataFromElement((Element) text.item(0)));

	        	NodeList matchType = element.getElementsByTagName("matchType");
	        	temp.put("matchType", XMLUtils.getCharacterDataFromElement((Element) matchType.item(0)));

	        	list.add(temp);
	        }
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }

		return list;
	}//getNegativeKeywordList	
	
	
	
	/*
	 * Get keyword by Id
	 */
	public Map<String, String> getKeywordById(){
		Map<String, String> keyword= new HashMap<String, String>();
		
		try {
	        
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
	        
	        for (int i = 0; i < nodes.getLength(); i++) {
	        	Element element = (Element) nodes.item(i);
	        	
	        	NodeList text = element.getElementsByTagName("text");
	        	keyword.put("text", XMLUtils.getCharacterDataFromElement((Element) text.item(0)));

	        	NodeList matchType = element.getElementsByTagName("matchType");
	        	keyword.put("matchType", XMLUtils.getCharacterDataFromElement((Element) matchType.item(0)));
	        	
	        	NodeList destinationUrl = element.getElementsByTagName("destinationUrl");
	        	keyword.put("destinationUrl", XMLUtils.getCharacterDataFromElement((Element) destinationUrl.item(0)));
	        	
	        	NodeList adGroupCriterionBids = element.getElementsByTagName("AdGroupCriterionBids.Type");
	        	String adGroupCriterionBidsValue = XMLUtils.getCharacterDataFromElement((Element) adGroupCriterionBids.item(0));
	        	keyword.put("adGroupCriterionBids", adGroupCriterionBidsValue);
	        	
		        NodeList maxCpc = element.getElementsByTagName("microAmount");
		        keyword.put("maxCpc", XMLUtils.getCharacterDataFromElement((Element) maxCpc.item(0)));

	        	NodeList bidSource = element.getElementsByTagName("bidSource");
	        	keyword.put("bidSource", XMLUtils.getCharacterDataFromElement((Element) bidSource.item(0)));
	        	

	        }

	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }		
		
		return keyword;
	}
}
